package com.example.houduan.Controller;

import com.example.houduan.Pojo.City;
import com.example.houduan.Service.CityService;
import com.example.houduan.Utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin
public class CityController {

    @Autowired
    CityService cityService;

    @RequestMapping("/getCityPage")
    @ResponseBody
    public PageModel getCityPage(@RequestParam int num, int size) {
        return cityService.getCityPage(num, size);
    }

    @RequestMapping("/addCity")
    @ResponseBody
    public String addCity(@RequestBody City city) {
        if (cityService.addCity(city) == 0) {
            return "添加失败";
        } else {
            return "添加成功";
        }
    }

    @RequestMapping("deleteCity")
    @ResponseBody
    public String deleteCity(Integer city_id) {
        if (cityService.deleteCity(city_id) == 0) {
            return "删除失败";
        } else {
            return "删除成功";
        }
    }

    @RequestMapping("updateCity")
    @ResponseBody
    public String updateCity(@RequestBody City city) {
        if (cityService.updateCity(city) == 0) {
            return "修改失败";
        } else {
            return "修改成功";
        }
    }

    @RequestMapping("/getCityByConditions")
    @ResponseBody
    public List<City> getCityByConditions(@RequestParam(required = false) Integer city_id, String city_name) {
        return cityService.getCityByConditions(city_id, city_name);
    }

    @RequestMapping("/getCityList")
    @ResponseBody
    public List<City> getCityList(){
        return cityService.getCityList();
    }
}
